<!--
 * @Author: Cenven
 * @Date: 2021-08-02 10:52:39
 * @Description: 北京泓深教育发展有限公司
 * @Descripttion: 
 * @version: @Lastest
 * @LastEditTime: 2021-08-02 15:02:52
 * @CopyRight: 北京泓深教育发展有限公司
-->
<template>
  <div class="page-mobile-comment">
    <white-header
      :isShowTopNav="true"
      :isShowBack="true"
      :isShowRight="true"
      title="课程评价"
    ></white-header>
    <div class="comment_container" v-if="!sendSuccess">
      <div class="course_info">
        <img class="icon_path" :src="course_obj.iconPath" alt="" />
        <div class="course_name">{{ course_obj.courseName }}</div>
      </div>
      <div class="comment_rate">
        <van-rate v-model="comment_obj.rate" :size="30" :gutter="8" />
        <p
          class="rate_text"
          :style="{
            color: [4, 5].includes(comment_obj.rate) ? '#ffd21e' : '#000'
          }"
        >
          {{ comment_obj.rate | formatRate }}
        </p>
      </div>
      <div class="comment_content">
        <van-field
          v-model="comment_obj.content"
          rows="10"
          type="textarea"
          maxlength="500"
          placeholder="满意您就夸一夸"
          show-word-limit
        />
      </div>
      <div
        :class="['submit_comment', comment_obj.rate != 0 ? 'active' : '']"
        @click="sendComment"
      >
        发布评价
      </div>
    </div>
    <div class="comment_container" v-else>
        <div class="course_info">
            <img class="icon_path" :src="course_obj.iconPath" alt="" />
            <div class="course_name">{{ course_obj.courseName }}</div>
        </div>
        <div class="success_box">
            <van-icon name="checked" color="var(--theme-color)" size="1.6rem"/>
            <p style="margin-bottom:.4rem;">发布评价成功</p>
            <span class="go_back" @click="goBack()">立即返回</span>
        </div>
    </div>
  </div>
</template>

<script>
import WhiteHeader from "~/components/mobile/WhiteHeader.vue";
import { getCourse, saveCourseComment } from "~/api/course";
import { mapState, mapActions, mapMutations } from "vuex";
export default {
  scrollToTop: true,
  components: {
    WhiteHeader
  },
  data() {
    return {
      that: this,
      comment_obj: {
        rate: 0,
        content: ""
      },
      course_obj: {},
      sendSuccess:false,
    };
  },
  computed: {
    ...mapState({
      user_info(state) {
        return state.user_info;
      },
      token(state) {
        return state.token;
      },
      is_loading(state) {
        return state.is_loading;
      }
    }),
    courseId() {
      return this.$route.query.courseId;
    }
  },
  watch: {},
  filters: {
    formatRate(val) {
      switch (val) {
        case 1:
          return "非常不满意";
        case 2:
          return "不满意";
        case 3:
          return "一般";
        case 4:
          return "满意";
        case 5:
          return "非常满意";
        default:
          return "";
      }
    }
  },
  activated() {
    if (process.client) {
      if (!this.token && Object.keys(this.user_info).length == 0) {
        this.$router.replace("/m/login");
        return;
      }
    }
    this.initPage();
  },
  methods: {
    ...mapMutations(["SET_IS_LOADING"]),
    goBack(){
        this.$router.replace('/m/course-my');
    },
    async getData() {
      this.SET_IS_LOADING(true);
      let result = await getCourse({
        id: this.courseId
      });
      if (result && result.data && result.data.code == 200) {
        const { data } = result.data;
        if (data && data.rows && data.rows.length > 0) {
          this.course_obj = data.rows[0];
        }
      } else {
        this.$toast(
          this.$t("locale.pc.mycourse.coursebuy.t7") + result &&
            result.data &&
            result.data.msg
        );
      }
      this.SET_IS_LOADING(false);
    },
    async sendComment() {
      if (this.comment_obj.rate == 0) {
        return;
      }
      this.SET_IS_LOADING(true);
      let params = {
        ...this.comment_obj,
        courseId: this.courseId
      };
      let result = await saveCourseComment(params);
      if (result && result.data && result.data.code == 200) {
        this.SET_IS_LOADING(false);
        this.sendSuccess = true;
      } else if (result && result.data && result.data.code == 1001) {
        this.SET_IS_LOADING(false);
        this.$toast(result.data.msg);
      } else {
        this.SET_IS_LOADING(false);
        this.$toast("发表失败！");
      }
    },
    formatDate(date) {
      var date = new Date(date);
      var YY = date.getFullYear() + "-";
      var MM =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var hh =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      var mm =
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":";
      var ss =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return YY + MM + DD;
      //   return YY + MM + DD + " " + hh + mm + ss;
    },
    initPage() {
      this.comment_obj = {
        rate: 0,
        content: ""
      };
      this.getData();
    }
  },
  created() {},
  mounted() {}
};
</script>

<style lang="scss" scoped>
.page-mobile-comment {
  width: 100vw;
  height: 100vh;
  background: #f4f4f4;
  padding: 0.288rem;
  box-sizing: border-box;
  .comment_container {
    width: 100%;
    background: #fff;
    box-sizing: border-box;
    border-radius: 0.1rem;
    padding: 0.2rem 0.2rem 0.4rem;
    .course_info {
      display: flex;
      justify-content: space-between;
      .icon_path {
        width: 1.5rem;
        height: 1rem;
        display: block;
        background: var(--theme-color);
      }
      .course_name {
        width: 4.5rem;
        line-height: 1rem;
        font-size: 0.3rem;
        color: #000;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
    .comment_rate {
      margin: 0.288rem 0;
      text-align: center;
      font-size: 0.2688rem;
      .rate_text {
        margin-top: 0.16rem;
        color: #000;
      }
    }
    .comment_content {
      background: #f4f4f4;
    }
    .submit_comment {
      width: 100%;
      height: 0.8rem;
      background: #f4f4f4;
      color: #ccc;
      text-align: center;
      line-height: 0.8rem;
      font-size: 0.3rem;
      border-radius: 0.2rem;
      margin-top: 0.4rem;
      &.active {
        background: var(--theme-color);
        color: #ffffff;
      }
    }
    .success_box{
        padding: .6rem;
        text-align: center;
        font-size: .3rem;
        line-height: .5rem;
        .go_back{
            text-decoration: underline;
            color: var(--theme-color);
        }
    }
  }
}
</style>
<style lang="scss">
.page-mobile-comment {
  .van-cell {
    padding: 0;
  }
  .van-field__control {
    background: #f4f4f4;
    padding: 0.1rem;
    font-size: 0.3rem;
    line-height: 0.4rem;
    // font-size: .2688rem;
  }
}
</style>
